#include "input_manager.h"
#include "stdio.h"
#include <linux/input.h>
#include <string.h>
#include <tslib.h>
#include <stdlib.h>
#include<config.h>

static int TouchScreenInputEventInit();
static int TouchScreenGetInput(char *);

static struct ts_sample_mt **samp_mt;
static struct ts_sample_mt **pre_samp_mt;
static struct tsdev *ts;
static int max_slots;
static T_InputEventOpr g_tTouchScreenOpr = {
    .name = "ts",
    .GetInput = TouchScreenGetInput,
    .InputEventInit = TouchScreenInputEventInit,

};

int TouchScreenInputEventInit()
{

    int i;

    // int point_pressed[20];
    // struct input_absinfo slot;
    ts = ts_setup(NULL, 0);
    if (!ts)
    {
        printf("ts_setup err\n");
        return -1;
    }
    // if (ioctl(ts_fd(ts), EVIOCGABS(ABS_MT_SLOT), &slot) < 0)
    // {
    // 	perror("ioctl EVIOGABS");
    // 	ts_close(ts);
    // 	return -1;
    // }

    max_slots = 1;

    samp_mt = malloc(sizeof(struct ts_sample_mt *));
    if (!samp_mt)
    {
        ts_close(ts);
        return -1;
    }
    samp_mt[0] = calloc(max_slots, sizeof(struct ts_sample_mt));
    if (!samp_mt[0])
    {
        free(samp_mt);
        ts_close(ts);
        return -1;
    }

    pre_samp_mt = malloc(sizeof(struct ts_sample_mt *));
    if (!pre_samp_mt)
    {
        ts_close(ts);
        return -1;
    }
    pre_samp_mt[0] = calloc(max_slots, sizeof(struct ts_sample_mt));
    if (!pre_samp_mt[0])
    {
        free(pre_samp_mt);
        ts_close(ts);
        return -1;
    }

    for (i = 0; i < max_slots; i++)
        pre_samp_mt[0][i].valid = 0;

    return 0;
}
static int direction(struct ts_sample_mt *pointA, struct ts_sample_mt *pointB)
{
    int x = pointB->x - pointA->x;
    if (x >= 5)
        return 1;
    else if (x <= -5)
        return -1;
    else
        return 0;
}
static int TouchScreenGetInput(char *event)
{
    int touch_cnt = 0;
    int ret = 0;

    while (1)
    {
        ret = ts_read_mt(ts, samp_mt, max_slots, 1);
        if (ret < 0)
        {
            printf("ts_read_mt err\n");
            ts_close(ts);
            return -1;
        }

        for (int i = 0; i < max_slots; i++)
        {
            if (samp_mt[0][i].valid && touch_cnt == 0)
            { 
                memcpy(&pre_samp_mt[0][i], &samp_mt[0][i], sizeof(struct ts_sample_mt));
            }
        }
        if (touch_cnt == 1)
        {
            for (int i = 0; i < max_slots; i++)
            {
                if (samp_mt[0][i].valid && samp_mt[0][i].tracking_id == -1)
                {
                     int dir = direction(&pre_samp_mt[0][0], &samp_mt[0][0]);
                    if (dir == 1)
                    {
                        *event = 'n';
                        DBG_PRINTF("directon: right \n");
                        return 0;
                    }
                    else if (dir == -1)
                    {
                        *event = 'u';
                         DBG_PRINTF("directon: left \n");
                        return 0;
                    }
                    else
                    {
                        *event = 'q';
                         DBG_PRINTF("directon: quit \n");
                        return 0;
                    }
                }
            }
        }
        if (samp_mt[0][0].valid&&samp_mt[0][0].tracking_id != -1)
        {
            touch_cnt =1;
        }
    }
}

int TouchScreenInit()
{
    g_tTouchScreenOpr.event = (char *)malloc(1);
    g_tTouchScreenOpr.InputEventInit();
    return RegisterInputEventOpr(&g_tTouchScreenOpr);
}